package test4_14.demo02;

public class Homework22_6 implements Runnable {

    private int totalTicket = 100;

    @Override
    public void run() {
        while (totalTicket > 0) {

            synchronized (this) {
                try {
                    if(totalTicket<=0){
                        break;
                    }
                    System.out.println(Thread.currentThread().getName() + "卖出了一张票，还剩" + --totalTicket + "张");
                    Thread.sleep(50);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            Thread.yield();
        }
    }
}


class test2 {
    public static void main(String[] args) throws InterruptedException {

        System.out.println("售票开始");

        Runnable runnable = new Homework22_6();

        Thread thread1 = new Thread(runnable, "宁波");
        Thread thread2 = new Thread(runnable, "四平");
        Thread thread3 = new Thread(runnable, "哈尔滨");
        Thread thread4 = new Thread(runnable, "北京");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();

        System.out.println("售票结束");
    }
}